// pages/login/login.js
import {login} from '../../api/login'
import {user} from '../../api/user'
import { createStoreBindings } from 'mobx-miniprogram-bindings'
import store from '../../store/index.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mobile:"",
    code:'',
    codeStyle:'fa',
    loginCode:'发送验证码',
    codeErr:"",
    mobileErr:'',
    beel:true,
    statusBarHeight:null
  },
  onLoad: function (options) {
    createStoreBindings(this, {
      store: store,
      fields: ['token'],
      actions: ['setToken']
    })
  },
  //手机号验证
  getMobileValue(e){
    this.setData({
      mobile:e.detail.value
    })
    if(!e.detail.value){
      this.setData({
        mobileErr:"手机号码不能为空"
      })
    }else if(e.detail.value!=='18900000000'){
      // e.detail.value.test(/^(?:(?:\+|00)86)?1[3-9]\d{9}$/)
      this.setData({
        mobileErr:"手机号码错误"
      })
    }else{
      this.setData({
        mobileErr:""
      })
    }
  },
  // 验证码
  getCodeValue(e){
    this.setData({
      code:e.detail.value
    })
    if(!e.detail.value){
      this.setData({
        codeErr:"验证码不能为空"
      })
    }else if(e.detail.value!=='246810'){
      this.setData({
        codeErr:"验证码错误"
      })
    }else{
      this.setData({
        codeErr:""
      })
    }
  },
  // 发起验证码
  fsong(){
    this.setData({
      codeStyle:'deng',
    })
    this.downTime(60)
  },
  // 倒计时封装
  async downTime(start,end=0){
    // 初始化事件
    if(this.data.beel){
      // console.log('开始');
      try{
        await user(18900000000)
      }catch(err){
        console.log(err.message);
      }
      let i = start
      this.setData({
        loginCode:`${i}s`,
        beel : false
      })
      const a = setInterval(() => {
        i = i-1
        this.setData({
          loginCode:`${i}s`
        })
        if(i < end){
          // console.log('结束');
          clearInterval(a)
          this.setData({
            loginCode:'发送验证码',
            codeStyle:'fa',
            beel : true
          })
        }
      }, 1000);
    }
  },
  // 登录
  async onLogin(){
    try{
      const {data} = await login({
        mobile:this.data.mobile,
        code:this.data.code
      })
      // console.log(data,1);
      wx:wx.setStorageSync('mykey', data)
      wx.switchTab({
        url: '/pages/home/home',
      })
      wx.showToast({
        title:'登录成功',
        icon:'success'
      })
    } catch (err){
      wx.switchTab({
        url: '/pages/home/home',
      })
      wx.showToast({
        title:'登录失败',
        icon:'error'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.getSystemInfo({
      success: (result) => {
        this.setData({
          statusBarHeight:result.statusBarHeight
        })
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})